package metadatabrowser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.layout.FillLayout;

import at.fhj.itm.jface.SchemaLabelProvider;
import at.fhj.itm.metadata.Column;
import at.fhj.itm.metadata.Schema;
import at.fhj.itm.metadata.Table;
import at.fhj.itm.metadata.impl.postgres.PostgresColumn;
import at.fhj.itm.refactor.Refactoring;
import at.fhj.itm.refactor.RefactoringSuite;
import at.fhj.itm.refactor.report.RefactoringReport;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import at.fhj.itm.jface.TableLabelProvider;
import at.fhj.itm.jface.MetadataItemLabelProvider;
import at.fhj.refactor.refactorings.SplitTable;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.jface.viewers.TableViewer;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import at.fhj.itm.jface.ReportItemTableLabelProvider;
import at.fhj.itm.jface.FancyColumnLabelProvider;
import org.eclipse.swt.layout.RowLayout;

public class SplitTableDialog extends Dialog {

	private RefactoringReport parameterReport;
	private RefactoringReport refactoringReport;
	
	private Text txtForeignKeyLeft;
	private Text txtTableNameRight;
	private Text txtPrimaryKeyColumnRight;
	private Text txtReferencingColLeft;

	private java.util.List<Column> leftColumns;
	private java.util.List<Column> rightColumns;
	

	private java.util.List<? extends Schema> schemas;
	private Schema selectedSchema;
	private Table selectedTable;
	private RefactoringSuite suite;
	private ComboViewer comboSchemaViewer;
	private ComboViewer comboTableViewer;
	private ListViewer listViewerLeft;
	private ListViewer listViewerRight;
	private Button btnMoveRight;
	private Button btnMoveLeft;
	private org.eclipse.swt.widgets.Table parameterReportTable;
	private org.eclipse.swt.widgets.Table refactoringReportTable;
	private SplitTable splitTable;
	private Text txtTableNameLeft;
	private Button cbAutoIncrementRight;
	private Button btnValidateParamters;
	private Button btnPrepareRefactoring;
	private Button btnRefactor;
	private TableViewer parameterReportViewer;
	private CTabFolder tabFolderReports;
	private Button btnColLeftUp;
	private Button btnColLeftDown;
	private TableViewer refactoringReportViewer;
	private Button btnBrowserDataScriptLocation;
	private Text txtDataMigrationScript;
	
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SplitTableDialog(Shell parentShell, final RefactoringSuite suite) {
		super(parentShell);
		this.suite = suite;
		this.schemas = suite.getMetadataProvider().getSchema();
		setShellStyle(SWT.SHELL_TRIM);
		parentShell.setText("Split Table");
		
		java.util.List<Refactoring> refactorings = suite.getRefactorings();
		for(Refactoring refactoring : refactorings){
			if(refactoring instanceof SplitTable){
				splitTable = (SplitTable) refactoring;
				break;
			}
		}
		
	}

	

	
	private void initColumnSplitLists(Table t){
		leftColumns = new ArrayList<Column>();
		rightColumns = new ArrayList<Column>();
		leftColumns.addAll(t.getColumns());
		
		
		
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Split Table");
	}
	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		parent.setFont(SWTResourceManager.getFont("Ubuntu", 8, SWT.NORMAL));
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(4, false));
		
		Label lblSchema = new Label(container, SWT.NONE);
		lblSchema.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblSchema.setText("Schema");
		
		comboSchemaViewer = new ComboViewer(container, SWT.READ_ONLY);
		comboSchemaViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent ev) {
				IStructuredSelection sel = (IStructuredSelection) ev.getSelection();
				Schema s = (Schema) sel.getFirstElement();
				schemaSelected(s);
			}
		});
		
		
		comboSchemaViewer.setContentProvider(ArrayContentProvider.getInstance());
		comboSchemaViewer.setInput(schemas);
		Combo comboSchema = comboSchemaViewer.getCombo();
		comboSchema.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		comboSchemaViewer.setLabelProvider(new MetadataItemLabelProvider());
		
		Label lblTable = new Label(container, SWT.NONE);
		lblTable.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTable.setText("Table");
		
		comboTableViewer = new ComboViewer(container, SWT.READ_ONLY);
		comboTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				if(selection.getFirstElement() == null){
					return;
				}
				
				Table table = (Table) selection.getFirstElement();
				tableSelected(table);
			}
		});

		Combo comboTables = comboTableViewer.getCombo();
		comboTables.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		comboTableViewer.setLabelProvider(new MetadataItemLabelProvider());
		comboTableViewer.setContentProvider(ArrayContentProvider.getInstance());

	
		
		Group grpLeftTable = new Group(container, SWT.NONE);
		grpLeftTable.setText("Left Table");
		grpLeftTable.setLayout(new GridLayout(2, false));
		grpLeftTable.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 4, 2));
		
		Label lblTableName = new Label(grpLeftTable, SWT.NONE);
		lblTableName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTableName.setText("Table Name:");
		
		txtTableNameLeft = new Text(grpLeftTable, SWT.BORDER);
		txtTableNameLeft.setText("customer");
		txtTableNameLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblPrimaryKeyColumnLeft = new Label(grpLeftTable, SWT.NONE);
		lblPrimaryKeyColumnLeft.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPrimaryKeyColumnLeft.setText("Primary Key Column:");
		
		Text txtPrimaryKeyColLeft = new Text(grpLeftTable, SWT.BORDER);
		txtPrimaryKeyColLeft.setText("id");
		txtPrimaryKeyColLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblReferencingColumn = new Label(grpLeftTable, SWT.NONE);
		lblReferencingColumn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblReferencingColumn.setText("Referencing Column");
		
		txtReferencingColLeft = new Text(grpLeftTable, SWT.BORDER);
		txtReferencingColLeft.setText("location_id");
		txtReferencingColLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblForeignKeyName = new Label(grpLeftTable, SWT.NONE);
		lblForeignKeyName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblForeignKeyName.setText("Foreign Key Name:");
		
		txtForeignKeyLeft = new Text(grpLeftTable, SWT.BORDER);
		txtForeignKeyLeft.setText("fk_department_location");
		txtForeignKeyLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Group grpRightTable = new Group(container, SWT.NONE);
		grpRightTable.setText("Right Table");
		grpRightTable.setLayout(new GridLayout(3, false));
		grpRightTable.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1));
		
		Label lblTableName_1 = new Label(grpRightTable, SWT.NONE);
		lblTableName_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblTableName_1.setText("Table Name:");
		
		txtTableNameRight = new Text(grpRightTable, SWT.BORDER);
		txtTableNameRight.setText("location");
		txtTableNameRight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblPrimaryKeyColumn_1 = new Label(grpRightTable, SWT.NONE);
		lblPrimaryKeyColumn_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPrimaryKeyColumn_1.setText("Primary Key Column:");
		
		txtPrimaryKeyColumnRight = new Text(grpRightTable, SWT.BORDER);
		txtPrimaryKeyColumnRight.setText("id");
		txtPrimaryKeyColumnRight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		cbAutoIncrementRight = new Button(grpRightTable, SWT.CHECK);
		cbAutoIncrementRight.setText("Auto increment");
		
		Group grpColumns = new Group(container, SWT.NONE);
		grpColumns.setText("Columns");
		grpColumns.setLayout(new GridLayout(5, false));
		grpColumns.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
		
		listViewerLeft = new ListViewer(grpColumns, SWT.BORDER | SWT.V_SCROLL);
		listViewerLeft.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel = (IStructuredSelection) event.getSelection();
				Column selColumn = (Column) sel.getFirstElement();		
				upateLeftColumnButtons(sel, selColumn);		
			}
		});
		List listColumnsLeft = listViewerLeft.getList();
		listColumnsLeft.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		listViewerLeft.setLabelProvider(new FancyColumnLabelProvider());
		listViewerLeft.setContentProvider(ArrayContentProvider.getInstance());
		
		Composite composite_1 = new Composite(grpColumns, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		composite_1.setLayout(new BorderLayout(0, 0));
		
		btnColLeftUp = new Button(composite_1, SWT.NONE);
		btnColLeftUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveOrder(leftColumns, listViewerLeft, true);
			}
		});
		btnColLeftUp.setLayoutData(BorderLayout.NORTH);
		btnColLeftUp.setText("Up");
		
		btnColLeftDown = new Button(composite_1, SWT.NONE);
		btnColLeftDown.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveOrder(leftColumns, listViewerLeft, false);
			}
		});
		btnColLeftDown.setLayoutData(BorderLayout.SOUTH);
		btnColLeftDown.setText("Down");
		Composite composite = new Composite(grpColumns, SWT.NONE);
		FillLayout fl_composite = new FillLayout(SWT.VERTICAL);
		fl_composite.spacing = 15;
		composite.setLayout(fl_composite);
		
		btnMoveRight = new Button(composite, SWT.NONE);
		btnMoveRight.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveColumnRight();
			}
		});
		btnMoveRight.setEnabled(false);
		btnMoveRight.setText("Move Right -->");
		
		btnMoveLeft = new Button(composite, SWT.NONE);
		btnMoveLeft.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveColumsnLeft();
			}
		});
		btnMoveLeft.setEnabled(false);
		btnMoveLeft.setText("<-- Move Left");
		
		Composite composite_2 = new Composite(grpColumns, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		composite_2.setLayout(new BorderLayout(0, 0));
		
		Button button = new Button(composite_2, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveOrder(rightColumns, listViewerRight, true);
			}
		});
		button.setText("Up");
		button.setLayoutData(BorderLayout.NORTH);
		
		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				moveOrder(rightColumns, listViewerRight, false);
			}
		});
		button_1.setText("Down");
		button_1.setLayoutData(BorderLayout.SOUTH);
		
		listViewerRight = new ListViewer(grpColumns, SWT.BORDER | SWT.V_SCROLL);
		listViewerRight.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel = (IStructuredSelection) event.getSelection();
				btnMoveLeft.setEnabled(sel.size()>0);
			}
		});
		List listColumnsRight = listViewerRight.getList();
		listColumnsRight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		tabFolderReports = new CTabFolder(container, SWT.BORDER);
		tabFolderReports.setTabHeight(20);
		tabFolderReports.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
		tabFolderReports.setSimple(false);

		
		CTabItem tbtmParameterValidation = new CTabItem(tabFolderReports, SWT.NONE);
		tbtmParameterValidation.setText("Parameter Validation");
		
		parameterReportViewer = new TableViewer(tabFolderReports, SWT.BORDER | SWT.FULL_SELECTION);
		parameterReportViewer.setColumnProperties(new String[] {});
		parameterReportTable = parameterReportViewer.getTable();
		parameterReportTable.setFont(SWTResourceManager.getFont("Ubuntu", 8, SWT.NORMAL));
		parameterReportTable.setLinesVisible(true);
		parameterReportTable.setHeaderVisible(true);
		parameterReportViewer.setContentProvider(ArrayContentProvider.getInstance());
		tbtmParameterValidation.setControl(parameterReportTable);
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(parameterReportViewer, SWT.NONE);
		TableColumn tblclmnTitle_1 = tableViewerColumn_2.getColumn();
		tblclmnTitle_1.setWidth(100);
		tblclmnTitle_1.setText("Title");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(parameterReportViewer, SWT.NONE);
		TableColumn tblclmnDetails_1 = tableViewerColumn_3.getColumn();
		tblclmnDetails_1.setWidth(100);
		tblclmnDetails_1.setText("Details");
		parameterReportViewer.setLabelProvider(new ReportItemTableLabelProvider());
		
		CTabItem tbtmRefactoringReport = new CTabItem(tabFolderReports, SWT.NONE);
		tbtmRefactoringReport.setText("Refactoring Report");
		
		refactoringReportViewer = new TableViewer(tabFolderReports, SWT.BORDER | SWT.FULL_SELECTION);
		refactoringReportTable = refactoringReportViewer.getTable();
		refactoringReportTable.setFont(SWTResourceManager.getFont("Ubuntu", 8, SWT.NORMAL));
		refactoringReportTable.setHeaderVisible(true);
		refactoringReportViewer.setContentProvider(ArrayContentProvider.getInstance());
		tbtmRefactoringReport.setControl(refactoringReportTable);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(refactoringReportViewer, SWT.NONE);
		TableColumn tblclmnTitle = tableViewerColumn.getColumn();
		tblclmnTitle.setWidth(100);
		tblclmnTitle.setText("Title");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(refactoringReportViewer, SWT.NONE);
		TableColumn tblclmnDetails = tableViewerColumn_1.getColumn();
		tblclmnDetails.setWidth(100);
		tblclmnDetails.setText("Details");
		refactoringReportViewer.setLabelProvider(new ReportItemTableLabelProvider());
		
		Composite composite_4 = new Composite(container, SWT.NONE);
		composite_4.setLayout(new GridLayout(3, false));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1));
		
		Label lblDataMigrationScript = new Label(composite_4, SWT.NONE);
		lblDataMigrationScript.setText("Data Migration Script:");
		
		txtDataMigrationScript = new Text(composite_4, SWT.BORDER);
		txtDataMigrationScript.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		btnBrowserDataScriptLocation = new Button(composite_4, SWT.NONE);
		btnBrowserDataScriptLocation.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectSqlDataScriptPath();
			}
		});
		btnBrowserDataScriptLocation.setText("Browse");
		
		Composite composite_3 = new Composite(container, SWT.NONE);
		composite_3.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite_3.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 4, 1));
		
		btnValidateParamters = new Button(composite_3, SWT.NONE);
		btnValidateParamters.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				validateRefactoringParameters();
			}

		});
		btnValidateParamters.setText("Validate Paramters");
		
		btnPrepareRefactoring = new Button(composite_3, SWT.NONE);
		btnPrepareRefactoring.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				prepareRefactoring();
			}
		});
		btnPrepareRefactoring.setEnabled(false);
		btnPrepareRefactoring.setText("Prepare Refactoring");
		
		btnRefactor = new Button(composite_3, SWT.NONE);
		btnRefactor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showSchemaRefactoring();
			}
		});
		btnRefactor.setEnabled(false);
		btnRefactor.setText("Refactor");
		
		Button btnMigrateData = new Button(composite_3, SWT.NONE);
		btnMigrateData.setText("Migrate Data");
		btnMigrateData.setEnabled(false);
		listViewerRight.setLabelProvider(new MetadataItemLabelProvider());
		listViewerRight.setContentProvider(new ArrayContentProvider());

		
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.CLOSE_ID, "Close",
				true);
	}

	private void tableSelected(Table t){
		initColumnSplitLists(t);
		listViewerLeft.setInput(leftColumns);
		listViewerRight.setInput(rightColumns);
		selectedTable = t;
	}
	
	private void moveColumnRight(){
		IStructuredSelection sel = (IStructuredSelection) listViewerLeft.getSelection();
		java.util.List<Column> selected = sel.toList();
		leftColumns.removeAll(selected);
		rightColumns.addAll(selected);
		listViewerLeft.refresh();
		listViewerRight.refresh();
		
	}
	private void moveColumsnLeft(){
		IStructuredSelection sel = (IStructuredSelection) listViewerRight.getSelection();
		java.util.List<Column> selected = sel.toList();
		rightColumns.removeAll(selected);
		leftColumns.addAll(selected);
		listViewerLeft.refresh();
		listViewerRight.refresh();
		
	}
	private void selectSqlDataScriptPath(){
		FileDialog fd = new FileDialog(this.getShell());
		fd.setFilterExtensions(new String[]{"*.sql"});
		String path = fd.open();
		txtDataMigrationScript.setText(path);
		
		
	}
	private void moveOrder(java.util.List<Column> list, ListViewer viewer, boolean up){
		IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
		if(sel.size() != 1){
			return;
		}
		Column column = (Column) sel.getFirstElement();
		int curIndex = list.indexOf(column);
		
		if(curIndex == -1){
			return;
		}
		
		if(up){
			if((curIndex + 1) < list.size())
			{
				Collections.swap(list, curIndex, curIndex +1);
			}
			
		}else{
			if((curIndex - 1) >= 0){
				Collections.swap(list, curIndex, curIndex-1);
			}
		}
		
		viewer.refresh();
		
		
		
	}
	
	private void validateRefactoringParameters(){
		applyGuiOnRefactoring();
		parameterReport = splitTable.validateParamters();
		boolean valid = parameterReport.isRefactoringValid();
		
		btnPrepareRefactoring.setEnabled(valid);
		
		parameterReportViewer.setInput(parameterReport.getReportItems());
		parameterReportViewer.refresh();
		
		
		
		
	}
	private void prepareRefactoring(){
		refactoringReport = splitTable.prepareRefactoring();
		refactoringReportViewer.setInput(refactoringReport.getReportItems());
		refactoringReportViewer.refresh();
		tabFolderReports.setSelection(1);
		btnRefactor.setEnabled(true);
	}
	private void applyGuiOnRefactoring(){
		
		splitTable.setTable(selectedTable);
		splitTable.setColumnLeft(leftColumns);
		splitTable.setColumnsRight(rightColumns);
		splitTable.setForeignKeyName(txtForeignKeyLeft.getText());
		splitTable.setReferencingColumnName(txtReferencingColLeft.getText());
		splitTable.setTableNameLeft(txtTableNameLeft.getText());
		
		splitTable.setTableNameRight(txtTableNameRight.getText());
		splitTable.setRightTablePrimaryKeyColumnName(txtPrimaryKeyColumnRight.getText());
		splitTable.setRightTablePrimaryKeyAutoincrement(cbAutoIncrementRight.getSelection());
	}
	
	private void schemaSelected(Schema s){
		if(s == null){
			return;
		}
		selectedSchema = s;
		comboTableViewer.setInput(selectedSchema.getTables());
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(636, 668);
	}


	private void showSchemaRefactoring(){
		SchemaRefactoringViewer refactoringViewer = new SchemaRefactoringViewer(getShell());
		refactoringViewer.setRefactoringSql(splitTable.generateRefactoringScript(suite.getCodeGenerator()));
		refactoringViewer.setBlockOnOpen(true);
		refactoringViewer.open();
		refactoringViewer.getShell().dispose();
	}

	private void upateLeftColumnButtons(IStructuredSelection sel,
			Column selColumn) {
		if(selColumn == null || selColumn.isPrimaryKeyColumn()){
			btnMoveRight.setEnabled(false);
			btnColLeftDown.setEnabled(false);
			btnColLeftUp.setEnabled(false);
		
		}
		else{
			btnColLeftDown.setEnabled(true);
			btnColLeftUp.setEnabled(true);
			btnMoveRight.setEnabled(sel.size() > 0);
		}
	}
}
